package com.blink.zkclient.operate;

import com.blink.base.TestZK;
import com.blink.zkclient.ZkClientCreator;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.junit.jupiter.api.Test;

/**
 * <Description> ZkClient获取节点数据</Description>
 * <ClassName> SetGetDataSample</ClassName>
 *
 * @Author liuxianzhao
 * @Date 2018年01月13日 13:46
 */
public class SetGetDataSample extends TestZK {
    @Test
    public void testSetData() {
        ZkClient zkClient = ZkClientCreator.getZkClient();
        String path = "/zk-book";
        zkClient.createEphemeral(path, new Integer(1));
        zkClient.writeData(path, new Integer(1));
        sleep();
    }

    @Test
    public void testGetData() {
        ZkClient zkClient = ZkClientCreator.getZkClient();
        String path = "/zk-book";
        zkClient.createEphemeral(path, "123");

        zkClient.subscribeDataChanges(path, new IZkDataListener() {
            @Override
            public void handleDataDeleted(String dataPath) throws Exception {
                logger.info("Node " + dataPath + " deleted.");
            }
            @Override
            public void handleDataChange(String dataPath, Object data) throws Exception {
                logger.info("Node " + dataPath + " changed, new data: " + data);
            }
        });
//        logger.info(zkClient.readData(path));
        zkClient.writeData(path, "456");
        sleep(1);
        zkClient.delete(path);
        sleep();
    }
}
